var phone = document.getElementById("phone");
var pwd = document.getElementById("pwd");
var btn = document.getElementById("btna");
var loginUrl = getUrl("loginPwd");
var phoneReg = /^1[0-9]{10}$/;
var pwdReg = /^(\w){6,20}$/;

function login() {
	if (phone.value != '') {
		if (!phoneReg.exec(phone.value)) {
			mui.toast('手机号填写不正确');
			phone.value = '';
		}
	}
	if (pwd.value != '') {
		if (!pwdReg.exec(pwd.value)) {
			mui.toast('密码填写有误');
			pwd.value = '';
		}
	}

	if (phoneReg.exec(phone.value) && pwdReg.exec(pwd.value)) {
		plus.nativeUI.showWaiting('正在登录...');
		var jsons = "account=" + phone.value + "&password=" + pwd.value;
		mui.ajax(loginUrl, {
			data: jsons,
			dataType: 'json',
			type: 'post',
			timeout: 3000,
			success: function(data) {
				//服务器返回响应，根据响应结果，分析是否登录成功；	
				if (data.code == 0) {
					console.log('响应结果' + JSON.stringify(data.obj));
					localStorage.account = writeData(data.obj.accountId);
					localStorage.nickName = writeData(data.obj.nickName);
					localStorage.phone = writeData(data.obj.account);
					localStorage.token = writeData(data.obj.token);
					localStorage.tokenExpire = writeData(data.obj.tokenExpire);
					localStorage.huanId = writeData(data.obj.huanId);
					localStorage.noticeToken = writeData(data.obj.noticeToken);
					refresh();
					setTimeout(function() {
						plus.nativeUI.closeWaiting();
						plus.webview.currentWebview().opener().close();
					}, 1000)

				} else {
					plus.nativeUI.closeWaiting();
					mui.toast(JSON.stringify(data.msg));
				}
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				plus.nativeUI.closeWaiting();
				//mui.toast(JSON.stringify(data.msg));
			}
		});
	}
}
//刷新
function refresh() {

	reloadHtml("home.html", "personal.html", "system.html", "guanzhu.html", "market.html", "brandBuying.html", "classifySearch.html", "productSearch.html", "hotSales.html","homeMarket.html");

}

//打开注册界面
function openRegister() {
	OpenNewWin({
		id: 'registerHead.html',
		url: 'registerHead.html'
	})
}
//打开忘记密码界面
function openPwd() {
	OpenNewWin({
		id: 'searchPwdHead.html',
		url: 'searchPwdHead.html'
	})
}
//打开短信验证登录界面
function openMeLogin() {
	mui.openWindow({
		url: 'messageLoginHead.html',
		id: 'messageLoginHead.html',
		waiting: {
			autoShow: false
		}
	});
}